export const messageToFrame = (windowCtx: Window, toWindowCtx: Window,
    request, time: number = 5000): Promise<string> => {
    return new Promise((resolve) => {
        let timeout = -1;

        const messageListener = ({ data }) => {
            clearTimeout(timeout);
            windowCtx.removeEventListener("message", messageListener);
            resolve(data);
        }

        timeout = setTimeout(() => {
            windowCtx.removeEventListener("message", messageListener);
            resolve(null);
        }, time);

        windowCtx.addEventListener("message", messageListener);
        toWindowCtx.postMessage(request, "*");
    });
}